package com.d3works.apps.account.db.repo;

import com.d3works.apps.account.db.InvoicePayment;
import com.d3works.apps.account.service.payment.invoice.payment.InvoicePaymentValidateService;
import com.d3works.exception.service.TraceBackService;
import com.d3works.inject.Beans;
import javax.persistence.PersistenceException;

public class InvoicePaymentManagementRepository extends InvoicePaymentRepository {

  @Override
  public InvoicePayment save(InvoicePayment invoicePayment) {
    try {
      if (invoicePayment.getStatusSelect() == InvoicePaymentRepository.STATUS_DRAFT) {
        Beans.get(InvoicePaymentValidateService.class).validate(invoicePayment);
      }
      return super.save(invoicePayment);
    } catch (Exception e) {
      TraceBackService.traceExceptionFromSaveMethod(e);
      throw new PersistenceException(e.getMessage(), e);
    }
  }
}
